#!/bin/bash

echo "+++++++++++++++++++++++ $(cd `dirname $0`; pwd)/$(basename $0) +++++++++++++++++++++++++"

# 当前脚本所在目录
v_d_current=$(cd `dirname $0`; pwd)
# 当前脚本的配置信息
v_f_settings=${v_d_current}/settings

# 检查配置文件是否存在
if [ ! -f ${v_f_settings} ]; then
    echo "settings file does not exist : $v_f_settings"
    exit -1
fi

# 加载配置信息
source ${g_f_util_properties} "load" ${v_f_settings}

# 检查安装目录是否存在
if [ ! -n "$install_dir" ] || [ ! -d ${install_dir} ]; then
    echo "create installation directory : $install_dir"
    mkdir -p ${install_dir}
fi

# 检查安装包是否存在
if [ ! -n "$install_file" ] || [ ! -f ${install_file} ]; then
    echo "installation file does not exist : $install_file"
    exit -1
fi

# 解压安装包到安装目录
tar -zxf ${install_file} -C ${install_dir}
cd ${install_dir}/zookeeper*
v_d_home=`pwd`
v_d_data=${v_d_home}/data
v_d_dataLog=${v_d_home}/dataLog
mkdir -p ${v_d_data} ${v_d_dataLog}
echo 1 > ${v_d_data}/myid
# zoo.cfg
\cp ${v_d_current}/zoo.cfg ${v_d_home}/conf
${g_f_util_replace} -s ${v_d_current}/zoo.cfg -t ${v_d_home}/conf/zoo.cfg dataDir=${v_d_data} dataLogDir=${v_d_dataLog}

# 设置开机启动
v_f_service_config=${v_d_current}/zookeeper-service
v_service="zookeeper"
v_f_service="/etc/init.d/$v_service"
touch "$v_f_service"
${g_f_util_replace} -s "$v_f_service_config" -t "$v_f_service" HOME=${v_d_home}
chmod 755 "$v_f_service"
chown root "$v_f_service"
chkconfig --add "$v_service"
chkconfig --levels 345 "$v_service" on

source /etc/profile
chkconfig --list | grep "$v_service"
exit -1